package com.example.sm.mycook;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.sm.mycook.db.SQLiteUtils;

public class AddressMessageActivityd extends AppCompatActivity implements View.OnClickListener {

    private ImageView mAddressBack;
    private EditText mAddressName;
    private EditText mAddressPhone;
    private TextView mAddressAddress;
    private EditText mAddressPath;
    private Button mBaocun;
    private EditText mAddressType;
    private CheckBox mIscheck;
    private SQLiteUtils sql;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_message);
        initView();
        sql=new SQLiteUtils(this);
    }

    private void initView() {
        mAddressBack = (ImageView) findViewById(R.id.address_back);
        mAddressName = (EditText) findViewById(R.id.address_name);
        mAddressPhone = (EditText) findViewById(R.id.address_phone);
        mAddressAddress = (TextView) findViewById(R.id.address_address);
        mAddressPath = (EditText) findViewById(R.id.address_path);
        mBaocun = (Button) findViewById(R.id.baocun);

        mAddressBack.setOnClickListener(this);
        mAddressPath.setOnClickListener(this);
        mAddressAddress.setOnClickListener(this);
        mBaocun.setOnClickListener(this);
        mAddressType = (EditText) findViewById(R.id.address_type);

        mIscheck = (CheckBox) findViewById(R.id.ischeck);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.address_back:
                finish();
                break;
            case R.id.baocun:
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                String check= mIscheck.isChecked() ? "1" : "2";
                User user = new User("1", mAddressName.getText().toString().trim(),
                        mAddressPhone.getText().toString().trim(),
                        mAddressType.getText().toString().trim(),
                        mAddressAddress.getText().toString().trim(),
                        mAddressPath.getText().toString().trim(),
                        check
                       );
                bundle.putSerializable("user", user);
                intent.putExtras(bundle);
                if("1".equals(check)){
                    sql.change();
                }
                setResult(RESULT_OK, intent);
                finish();
                break;

            case R.id.address_address:
                startActivityForResult(new Intent(this, MapLocationActivity.class), 1);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == RESULT_OK && data != null) {
            String Title = data.getStringExtra("Title");
            String City = data.getStringExtra("City");
            mAddressAddress.setText(City + "，" + Title);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(sql!=null){
            sql.getHelper().close();
            sql.getDB().close();
        }
    }
}
